//
//  BeeGroupListCell.m
//  FbeeAPP
//
//  Created by dev-m on 2018/2/28.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "BeeGroupListCell.h"
#import "BeePhotoImageView.h"
#import "BeeGroupListObj.h"

@interface BeeGroupListCell()
{
    BeePhotoImageView *photo;
    BeeGroupListCellType cellType;
    UILabel *groupName;
    UILabel *groupInfo;
    
    UIImageView *arrow;
    UIButton *cellBtn;
}

@end

@implementation BeeGroupListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier type:(BeeGroupListCellType)type
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        cellType = type;
        [self buildView];
    }
    
    return self;
}

-(void)buildView
{
    photo = [[BeePhotoImageView alloc] initWithFrame:CGRectMake(15, (BEELISTCELLHEIGHT -  35*UISCALE)/2, 35*UISCALE, 35*UISCALE) photoStyle:BeePhotoCellStyle];
    [self addSubview:photo];
    
    if (cellType == MyBeegroupType) {
        arrow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 9, 16)];
        arrow.image = [UIImage imageNamed:@"cell_arrow_r"];
        arrow.right = IPHONEWIDTH - 25;
        arrow.top = (BEELISTCELLHEIGHT - 16)/2;
        [self addSubview:arrow];
        
        photo.left += 15;
    }
    
    if (cellType == ReBeegroupType) {
        
        cellBtn = [UIButton makeButton:^(TTButtonExtend *make) {
            make.setFrame(CGRectMake(0, 0, 70, 30)).addBackGoundColor(FBEE_COLOR_PLACEHOLDER);
            make.layer.cornerRadius = 8;
            make.top = (BEELISTCELLHEIGHT - 30)/2;
            make.right = IPHONEWIDTH - 15;
            make.addTarget(self,@selector(joinBeeGroup));
            make.addTitleColor(UIColorFromRGB(0x744A05),UIControlStateNormal).addFont([UIFont systemFontOfSize:13.0f]);
            [self addSubview:make];
        }];
        
    }
    
    groupName = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.setFrame(CGRectZero).addFont([UIFont boldSystemFontOfSize:16.0f]).addTextColor(FBEE_COLOR_THEME);
        make.addBackGoundColor(FBEE_COLOR_PLACEHOLDER);
        make.height = 17;
        make.width = 64*UISCALE;
        make.left = photo.right +10;
        make.top = photo.top;
        [self addSubview:make];
    }];
    
    groupInfo = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.setFrame(CGRectZero).addFont([UIFont boldSystemFontOfSize:13.0f]).addTextColor(FBEE_COLOR_UNTOUCH);
        make.addBackGoundColor(FBEE_COLOR_PLACEHOLDER);
        make.height = 11;
        make.width = 180*UISCALE;
        [self addSubview:make];
    }];
    groupInfo.left = photo.right +10;
    groupInfo.top = groupName.bottom + 5;
    
    self.backgroundColor = [UIColor clearColor];
    
}

-(void)setDataObj:(BeeGroupListObj *)dataObj
{
    if (!dataObj.groupId || [NSString isEmptyString:dataObj.groupId]) {
        groupName.width = 64*UISCALE;
        groupInfo.width = 180*UISCALE;
        groupName.backgroundColor = FBEE_COLOR_PLACEHOLDER;
        groupInfo.backgroundColor = FBEE_COLOR_PLACEHOLDER;
        
        _dataObj = dataObj;
        
        return;
    }

    //基本数据
    {
        [photo setUrl:dataObj.logoUrl];
        
        groupName.text = dataObj.groupName;
        groupName.backgroundColor = [UIColor clearColor];
        
        groupInfo.text = dataObj.groupInfo;
        groupInfo.backgroundColor = [UIColor clearColor];
        groupInfo.textColor = FBEE_COLOR_UNTOUCH;
    }

    if (dataObj.isReward) {
        groupInfo.textColor = UIColorFromRGB(0xF5A623);
        groupInfo.text = dataObj.groupRewardInfo;
    }
    
    if (cellType == MyBeegroupType) {
        groupName.width = arrow.left - groupName.left - 30;
        groupInfo.width = arrow.left - groupInfo.left  - 30;
    }
    
    if (cellType == ReBeegroupType) {
        groupName.width = cellBtn.left - groupName.left  - 30;
        groupInfo.width = cellBtn.left - groupInfo.left - 30;
        cellBtn.backgroundColor = FBEE_COLOR_ASSISTANT;
        [cellBtn setTitle:NSLocalizedString(@"mAdd", nil) forState:UIControlStateNormal];
    }
    
    _dataObj = dataObj;
}

-(void)joinBeeGroup
{
    if (![NSString isEmptyString:self.dataObj.groupId]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_JoinBeeGroup object:self.dataObj.groupId];
    }
}

@end
